package com.lucifer.treasure.enchant.effect;

import com.lucifer.treasure.config.ConfigHandler;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.ActionResult;

/**
 * 毒液附魔效果
 * 攻击敌人时施加中毒效果
 */
public class VenomEnchantmentEffect {

    /**
     * 初始化毒液附魔效果监听器
     */
    public static void init() {
        AttackEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
            if (world.isClient || hand == null) {
                return ActionResult.PASS;
            }

            // 检查附魔是否启用
            if (!ConfigHandler.isEnchantEnabled("venom")) {
                return ActionResult.PASS;
            }

            // 检查目标是否为生物实体
            if (!(entity instanceof LivingEntity target)) {
                return ActionResult.PASS;
            }

            // 获取主手物品的毒液附魔等级
            var itemStack = player.getStackInHand(hand);
            if (itemStack.isEmpty()) {
                return ActionResult.PASS;
            }

            // 检查是否有毒液附魔
            var enchantments = EnchantmentHelper.getEnchantments(itemStack);
            int venomLevel = 0;

            for (var entry : enchantments.getEnchantmentEntries()) {
                RegistryEntry<?> enchantment = entry.getKey();
                if (enchantment.getIdAsString().equals("lucifer_treasure:venom")) {
                    venomLevel = entry.getIntValue();
                    break;
                }
            }

            if (venomLevel > 0) {
                // 计算中毒效果
                // 等级1: 毒液I 持续3秒
                // 等级2: 毒液II 持续6秒  
                // 等级3: 毒液III 持续9秒
                int duration = venomLevel * 60; // 3秒 = 60刻
                int amplifier = venomLevel - 1; // 等级1=毒液I(强度0)，等级2=毒液II(强度1)，等级3=毒液III(强度2)

                // 施加中毒效果
                StatusEffectInstance poisonEffect = new StatusEffectInstance(
                        StatusEffects.POISON,
                        duration,
                        amplifier,
                        false, // ambient
                        true,  // showParticles
                        true   // showIcon
                );

                target.addStatusEffect(poisonEffect);
            }

            return ActionResult.PASS;
        });
    }
} 